/**
 * 
 */
package com.cms.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


import com.action.DefaultAction;
import com.cms.manager.CpInfoManager;
import com.cms.model.AppInfo;
import com.cms.model.AppPointEntry;
import com.cms.model.AppPointOfBilling;
import com.cms.model.AppPointTerraceInfo;
import com.cms.model.CpInfo;
import com.cms.model.TerraceInfo;
import com.cms.service.AppInfoService;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.AppPointOfBillingService;
import com.cms.service.AppPointOfBillingServiceImpl;
import com.cms.service.TerraceInfoService;
import com.cms.service.TerraceInfoServiceImpl;
import com.util.Util;


public class AppPointOfBillingAction extends DefaultAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	
	public AppPointOfBillingAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<AppPointOfBilling> list;
	private AppPointOfBilling       model;
	private String        type;
	AppInfo appinfo;
	CpInfo cp;
	AppPointOfBillingService service = new AppPointOfBillingServiceImpl();
	AppInfoService appservice = new AppInfoServiceImpl();
	String msg;
	int cpid;
	int categoryparentid;
	int categoryid;
	String appName;
	//平台信息
	private List<TerraceInfo> terraceNameList;
	private TerraceInfoService terraceservice = new TerraceInfoServiceImpl();
	private String terraceIds;
	private List<AppPointTerraceInfo> terracelist;
	// 应用版本平台的关联
	private List<AppPointTerraceInfo> appPointTerraceInfos;
	private int[] ckb_province;
	private Map<Integer,List<AppPointOfBilling>> map;
	private List<AppPointEntry> entrylist;
	public String list()
	{
		logger.debug("list AppPointOfBillingAction");
		appinfo = appservice.getAppInfoByAppNumber(appinfo.getAppnumber());
		cp = CpInfoManager.getInstance().getCpInfo(appinfo.getCpid());
		list = service.getAppPointOfBillingOfAppNumber(appinfo.getAppnumber());
		map = new LinkedHashMap<Integer, List<AppPointOfBilling>>();
		AppPointEntry entrys = new AppPointEntry();
		entrys.setAppnumber(appinfo.getAppnumber());
		entrylist = service.getPointEntryByModel(entrys);
		for (AppPointEntry item : entrylist) {
			map.put(item.getId(), new ArrayList<AppPointOfBilling>());
		}
		for (AppPointOfBilling item : list) {
			int entry = item.getEntryid();
			if(!map.containsKey(entry)){
				map.put(entry, new ArrayList<AppPointOfBilling>());
			}
			map.get(entry).add(item);
		}
		return SUCCESS;
	}
	
	public String show()
	{
		appinfo = appservice.getAppInfoByAppNumber(appinfo.getAppnumber());
		cp = CpInfoManager.getInstance().getCpInfo(appinfo.getCpid());
		AppPointEntry entrys = new AppPointEntry();
		entrys.setAppnumber(appinfo.getAppnumber());
		entrylist = service.getPointEntryByModel(entrys);
		if (model.getId() >= 0)
		{
			model = service.getAppPointOfBilling(model.getId());
			type = "update";
			//平台列表
			/*terraceNameList = terraceservice.getQuelyTerraceNames();
			// 根据渠道ID查看渠道平台关联信息
			terraceIds = ",";
			terracelist = service.getByAppPointId(model.getId());		
			for (AppPointTerraceInfo e : terracelist) {
				System.out.println("e.getRelevanceid()=="+e.getRelevanceid());
				if (e.getRelevanceid()!=null && e.getRelevanceid().length()>0){
					
				terraceIds += e.getTerraceId() + ",";
				}
			}			
			appPointTerraceInfos = new ArrayList<AppPointTerraceInfo>();*/
		}		
		else
		{
			model.setId(-1);
			model.setAppnumber(appinfo.getAppnumber());
			if(cp != null){
				model.setCpid(cp.getId());
			}
			type = "insert";
			/*terraceNameList = terraceservice.getQuelyTerraceNames();
			// 根据平台id分配应用计费点平台的ID
			terracelist = new ArrayList<AppPointTerraceInfo>();
			for (TerraceInfo terrace : terraceNameList) {
				AppPointTerraceInfo tmp = new AppPointTerraceInfo();
				tmp.setTerraceId(terrace.getId());
				tmp.setId(-1);
				tmp.setOperatorsName(terrace.getOperatorsName());
				terracelist.add(tmp);
			}
			terraceIds = ",";
			appPointTerraceInfos = new ArrayList<AppPointTerraceInfo>();*/
		}

		return SUCCESS;
	}
	
	public AppPointTerraceInfo getAppPointTerraceInfo(AppPointOfBilling appPoint,
			TerraceInfo terrace) {
		for (AppPointTerraceInfo e : appPointTerraceInfos) {
			if (e.getApppointId() == appPoint.getId()
					&& e.getTerraceId() == terrace.getId())
				return e;
		}
		return null;
	}
	
	public String save()
	{
		appinfo = appservice.getAppInfoByAppNumber(appinfo.getAppnumber());
		cp = CpInfoManager.getInstance().getCpInfo(appinfo.getCpid());
		model.setCpid(appinfo.getCpid());
		if(type.equals("insert"))
		{
			model.setCreatedate(Util.dateToString(new Date()));
			model.setCreateuser(getAdminLoginName());
			//判断code是否存在，存在就直接返回
			List<AppPointOfBilling> li = service.getAppPointOfBillingOfAppNumber(model.getAppnumber(),model.getCode());
			if(li!=null && li.size()>0)
			{
				addActionError("计费点代码已经存在！");
				return SUCCESS;
			}
		}
		
		boolean fl = service.saveAppPointOfBilling(type, model);
		
		if(type.equals("insert"))
		{
			/*List<AppPointTerraceInfo> list = new ArrayList<AppPointTerraceInfo>();
			if (appPointTerraceInfos != null && ckb_province != null) {
				for (AppPointTerraceInfo e : appPointTerraceInfos) {
					for (int n = 0; n < ckb_province.length; n++) {
						if (e.getTerraceId() == ckb_province[n]) {
							list.add(e);
							continue;
						}
					}
				}
			}*/
			if(fl)
				msg = "计费点管理：添加成功";
			else
				msg = "计费点管理：添加失败！请重新添加！";
		}
		else if(type.equals("update"))
		{
			/*List<AppPointTerraceInfo> list = new ArrayList<AppPointTerraceInfo>();
			if (appPointTerraceInfos != null && ckb_province != null) {
				for (AppPointTerraceInfo e : appPointTerraceInfos) {
					for (int n = 0; n < ckb_province.length; n++) {
						if (e.getTerraceId() == ckb_province[n]) {
							list.add(e);
							continue;
						}
					}
				}
			}*/
			if(fl)
				msg = "计费点管理：更新成功！";
			else
				msg = "计费点管理：更新失败！请重新更新！";
			type = "upd";
		}
		else if(type.equals("delete"))
		{
			if(fl)
				msg = "计费点管理：删除成功！";
			else
				msg = "计费点管理：删除失败！请重新删除！";
		}
		return SUCCESS;
	}
	public String checkcode()
	{
		int num = 0;
		List<AppPointOfBilling> li = service.getAppPointOfBillingOfAppNumber(model.getAppnumber(),model.getCode());
		if(li!=null)
		{
			num = li.size();
		}
		
		return out(""+num);
	}
	private String out(String s)
	{
		try {
			setContentLength(s.getBytes("utf-8").length);
			setContentType("text/plain;charset=utf-8");
			PrintWriter pw = getResponse().getWriter();
			pw.write(s);
			pw.flush();
			pw.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	public List<AppPointOfBilling> getList() {
		return list;
	}

	public void setList(List<AppPointOfBilling> list) {
		this.list = list;
	}

	public AppPointOfBilling getModel() {
		return model;
	}

	public void setModel(AppPointOfBilling model) {
		this.model = model;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public AppInfo getAppinfo() {
		return appinfo;
	}

	public void setAppinfo(AppInfo appinfo) {
		this.appinfo = appinfo;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public CpInfo getCp() {
		return cp;
	}

	public void setCp(CpInfo cp) {
		this.cp = cp;
	}

	public int getCpid() {
		return cpid;
	}

	public void setCpid(int cpid) {
		this.cpid = cpid;
	}

	public int getCategoryparentid() {
		return categoryparentid;
	}

	public void setCategoryparentid(int categoryparentid) {
		this.categoryparentid = categoryparentid;
	}

	public int getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(int categoryid) {
		this.categoryid = categoryid;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public List<TerraceInfo> getTerraceNameList() {
		return terraceNameList;
	}

	public void setTerraceNameList(List<TerraceInfo> terraceNameList) {
		this.terraceNameList = terraceNameList;
	}

	public TerraceInfoService getTerraceservice() {
		return terraceservice;
	}

	public void setTerraceservice(TerraceInfoService terraceservice) {
		this.terraceservice = terraceservice;
	}

	public String getTerraceIds() {
		return terraceIds;
	}

	public void setTerraceIds(String terraceIds) {
		this.terraceIds = terraceIds;
	}

	public List<AppPointTerraceInfo> getTerracelist() {
		return terracelist;
	}

	public void setTerracelist(List<AppPointTerraceInfo> terracelist) {
		this.terracelist = terracelist;
	}

	public int[] getCkb_province() {
		return ckb_province;
	}

	public void setCkb_province(int[] ckb_province) {
		this.ckb_province = ckb_province;
	}

	public List<AppPointTerraceInfo> getAppPointTerraceInfos() {
		return appPointTerraceInfos;
	}

	public void setAppPointTerraceInfos(
			List<AppPointTerraceInfo> appPointTerraceInfos) {
		this.appPointTerraceInfos = appPointTerraceInfos;
	}

	public Map<Integer, List<AppPointOfBilling>> getMap() {
		return map;
	}

	public void setMap(Map<Integer, List<AppPointOfBilling>> map) {
		this.map = map;
	}

	public List<AppPointEntry> getEntrylist() {
		return entrylist;
	}

	public void setEntrylist(List<AppPointEntry> entrylist) {
		this.entrylist = entrylist;
	}
	
	
}
